from sqlalchemy.orm import Session

import models,schemas

def get_user(db:Session,user_id:int):
    return db.query(models.User).filter(models.User.user_id == user_id).first()


def get_users(db:Session,skip:int = 0,limit:int = 100):
    return db.query(models.User).offset(skip).limit(limit).all()

def add_user(db:Session,user:schemas.User):
    fake_hashed_password = user.password + "notreallyhashed"
    db_user = models.User(id=user.user_id,phone=user.phone,nickName=user.nickName,roomInfo=user.roomInfo,hashed_password=fake_hashed_password)
    db.add(db_user)
    db.commit()
    db.refresh(db_user)
    return db_user

def get_user_roomInfo(db:Session,user_id:int):
    return db.query(models.RoomInfo).filter(models.RoomInfo.owner_id==user_id).all()

def get_user_roomInfo_by_rid(db:Session,user_id:int,rid:int):
    return db.query(models.RoomInfo).filter(models.RoomInfo.rid==rid&models.RoomInfo.owner_id==user_id).all()

def add_user_roomInfo(db:Session,roomInfo:schemas.RoomInfo,user_id:int):
    db_roomInfo = models.RoomInfo(**roomInfo.dict(),owner_id=user_id)
    db.add(db_roomInfo)
    db.commit()
    db.refresh(db_roomInfo)
    return db_roomInfo